<%-- 
    Document   : upload
    Created on : 2017-11-21, 17:46:19
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.File"%>

<%
    request.setCharacterEncoding("utf-8");
    if (request.getContentType() != null) {     //判断内容接收内容不为空时
        DiskFileItemFactory factory = new DiskFileItemFactory();  //创建对象
        factory.setSizeThreshold(500);  //设置上传文件大小（单位字节）
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(404800);  //设置上传文件最大文件大小
        List<FileItem> forms = upload.parseRequest(request);
        for (FileItem form : forms) {
            File file = new File(request.getServletContext().getRealPath("/") + "upfile/2.jpg");  //定义文件保存路径（项目目录下的build/web/upfile/...）
            form.write(file);   //保存文件
        }
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form method="post" enctype="multipart/form-data">
            文件名：<input type="text" name="file_name" />
            <input type="file" name="file"/>
            <button>上传</button>
        </form>

    </body>
</html>

